
package jp.inaka.mironal.android.tad.content.provider;

import java.util.HashMap;

import jp.inaka.mironal.android.tad.content.ContentOpenHelper;
import jp.inaka.mironal.android.tad.content.table.LogTable;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class LogProvider extends ContentProvider {

    private static final int LOG = 1;

    private static final int LOG_ID = 2;

    private static UriMatcher URI_MATCHER;

    private static HashMap<String, String> sLogProjectionMap;

    static {
        URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
        URI_MATCHER.addURI(LogTable.AUTHORITY, LogTable.PATH, LOG );
        URI_MATCHER.addURI(LogTable.AUTHORITY, LogTable.PATH + "/#", LOG_ID);

        sLogProjectionMap = new HashMap<String, String>();
        sLogProjectionMap.put(LogTable._ID, LogTable._ID);
        sLogProjectionMap.put(LogTable.TASK_ID, LogTable.TASK_ID);
        sLogProjectionMap.put(LogTable.DAY_ID, LogTable.DAY_ID );
        sLogProjectionMap.put(LogTable.BEGIN, LogTable.BEGIN);
        sLogProjectionMap.put(LogTable.END, LogTable.END);

    }

    private ContentOpenHelper mOpenHelper;

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        // TODO 自動生成されたメソッド・スタブ
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        // TODO 自動生成されたメソッド・スタブ
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        int match = URI_MATCHER.match(uri);
        if( match == LOG ){
            ContentValues values;
            if( initialValues == null ){
                values = new ContentValues();
            }else{
                values = new ContentValues(initialValues);
            }

            if( values.containsKey(LogTable.BEGIN) == false ){
                throw new IllegalArgumentException();
            }

            if( values.containsKey(LogTable.END) == false ){
                throw new IllegalArgumentException();
            }

            if( values.containsKey(LogTable.DAY_ID) == false ){
                throw new IllegalArgumentException();
            }

            if( values.containsKey(LogTable.TASK_ID) == false ){
                throw new IllegalArgumentException();
            }


            long rowId;
            try {
                rowId = db.insertOrThrow(ContentOpenHelper.LOG_TABLE, null, values);
                if( rowId > 0 ){
                    Uri logUri = ContentUris.withAppendedId(LogTable.CONTENT_URI, rowId);
                    getContext().getContentResolver().notifyChange(logUri, null);
                    return logUri;
                }else{
                    throw new SQLException( "Failed to insert row into " + uri );
                }
            }catch (SQLiteConstraintException e) {
                return null;

            }
        }else{
            throw new IllegalArgumentException();
        }
    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new ContentOpenHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(ContentOpenHelper.LOG_TABLE);
        qb.setProjectionMap(sLogProjectionMap);

        switch (URI_MATCHER.match(uri)) {
            case LOG:

                break;

            case LOG_ID:
                qb.appendWhere(LogTable._ID
                            + " = "
                            + uri.getPathSegments().get(1) );

                break;
            default:
                throw new IllegalArgumentException();
        }

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        // TODO 自動生成されたメソッド・スタブ
        return 0;
    }

}
